﻿using System.Collections.Generic;
using System.Windows.Input;

// Author: Miroslav Matula m.matula@alutam.cz 
// Date : 31/8/2010

namespace System.Windows.Controls.DataGridToolBarControl
{
	public partial class FilterParameterUC : UserControl
	{
		#region -- Ctor ----------------

		public FilterParameterUC()
		{
			InitializeComponent();
			this.KeyDown += new KeyEventHandler(maFilterParameterUC_KeyDown);
			this.Loaded += new RoutedEventHandler(maFilterParameterUC_Loaded);
			buOK.Click += new RoutedEventHandler(buOK_Click);
			buOKPlus.Click += new RoutedEventHandler(buOKPlus_Click);
			buCancel.Click += new RoutedEventHandler(buCancel_Click);
			comLabel.SelectionChanged += new SelectionChangedEventHandler(comLabel_SelectionChanged);
		}

		#endregion

		#region -- Nested Class -------------
		
		public class ItemClass
		{
			public string Mark { get; set; }
			public string Text { get; set; }
			public FilterOperator_DGTB Value { get; set; }
			public override string ToString()
			{
				return Mark;
			}
		}

		#endregion

		#region -- Fields ------------------
		private DataGridToolBarFilterItemInternal _lastfilterItem;

		#endregion

		#region -- Properties ------------------

		public bool DialogResult { get; set; }
		public Type DataType { get; private set; }
		public string ColumnName
		{
			get
			{
				string res = null;
				if(comLabel.SelectedIndex != -1) {
					res = ((DataGridToolBarFilterItemInternal)comLabel.SelectedItem).ColumnName;
				}
				return res;
			}
		}
		public string Title
		{
			get
			{
				string res = null;
				if(comLabel.SelectedIndex != -1) {
					res = ((DataGridToolBarFilterItemInternal)comLabel.SelectedItem).Title;
				}
				return res;
			}
		}
		public FilterOperator_DGTB Operator { get { return ((ItemClass)comOperator.SelectedItem).Value; } }
		public string Mark { get { return ((ItemClass)comOperator.SelectedItem).Mark; } }
		public bool AddFilterDefToFilter { get; private set; } 

		#endregion

		#region -- Method ------------------------------

		internal void InitForm(List<DataGridToolBarFilterItemInternal> filterItems_, DataGridToolBarFilterItemInternal currentItem_, string textValue_)
		{
			if(filterItems_.Count == 0) return;

			comLabel.DisplayMemberPath = "Title";
			comLabel.ItemsSource = filterItems_;

			if(currentItem_ == null) currentItem_ = _lastfilterItem;
			if(currentItem_ != null) {
				comLabel.SelectedItem = currentItem_;
			}
			if(comLabel.SelectedItem == null) comLabel.SelectedIndex = 0;

			txtValue.Text = textValue_;
			txtValue.SelectionStart = 1;
			txtValue.SelectionLength = 0;
		}

		public void RunCancel()
		{
			DialogResult = false;
			ClosePopup();
		}

		public void RunOK(bool addToFilter)
		{
			//TODO:Validace
			AddFilterDefToFilter = addToFilter;
			_lastfilterItem = comLabel.SelectedItem as DataGridToolBarFilterItemInternal;
			DialogResult = true;
			ClosePopup();
		}

		private void ClosePopup()
		{
			if(this.Parent is System.Windows.Controls.Primitives.Popup) {
				((System.Windows.Controls.Primitives.Popup)Parent).IsOpen = false;
			}
		}

		#endregion

		#region --  Event Action ---------------------------

		void buCancel_Click(object sender, RoutedEventArgs e)
		{
			RunCancel();
		}

		void buOK_Click(object sender, RoutedEventArgs e)
		{
			RunOK(false);
		}

		void buOKPlus_Click(object sender, RoutedEventArgs e)
		{
			RunOK(true);
		}

		void maFilterParameterUC_Loaded(object sender, RoutedEventArgs e)
		{
			DialogResult = false;
			AddFilterDefToFilter = false;
			comOperator.SelectedIndex = 0;
			txtValue.Focus();
		}

		void maFilterParameterUC_KeyDown(object sender, KeyEventArgs e)
		{
			if(e.Key == Key.Escape) {
				e.Handled = true;
				RunCancel();
			} else if(e.Key == Key.Enter) {
				e.Handled = true;
				bool add = (Keyboard.Modifiers & ModifierKeys.Control) != 0;
				RunOK(add);
			}
		}

		private void comLabel_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			var selItem = comLabel.SelectedItem as DataGridToolBarFilterItemInternal;
			bool isString = selItem.DataType == typeof(string);

			var items = new List<ItemClass>();
			if(isString) items.Add(new ItemClass() { Mark = "-", Text = LangResources.LocalizedStrings.DGTB_Operator_Contains, Value = FilterOperator_DGTB.Contains });
			if(isString) items.Add(new ItemClass() { Mark = "|-", Text = LangResources.LocalizedStrings.DGTB_Operator_StartsWith, Value = FilterOperator_DGTB.StartsWith });
			if(isString) items.Add(new ItemClass() { Mark = "-|", Text = LangResources.LocalizedStrings.DGTB_Operator_EndsWith, Value = FilterOperator_DGTB.EndsWith });
			items.Add(new ItemClass() { Mark = "=", Text = LangResources.LocalizedStrings.DGTB_Operator_IsEqualTo, Value = FilterOperator_DGTB.IsEqualTo });
			items.Add(new ItemClass() { Mark = "<=", Text = LangResources.LocalizedStrings.DGTB_Operator_IsLessThanOrEqualTo, Value = FilterOperator_DGTB.IsLessThanOrEqualTo });
			items.Add(new ItemClass() { Mark = ">=", Text = LangResources.LocalizedStrings.DGTB_Operator_IsGreaterThanOrEqualTo, Value = FilterOperator_DGTB.IsGreaterThanOrEqualTo });
			comOperator.ItemsSource = items;
			comOperator.SelectedIndex = 0;
		}

		#endregion

	}
}
